<!doctype html>
<html>
	<head>
	<meta charset="utf-8">
	<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0"/>
	<title>注册</title>
	<link rel="stylesheet" href="css/login1.css" />
	<script type="text/javascript" src="js/zepto.min.js"></script>
	<script type="text/javascript" src="js/util.js"></script>
	</head>
	
	<body>
	
	<div class="login">
		<div class="welcome"><img src="img/login/welcome.png"></div>
		<div class="login-form">
			<form>
				<div class="login-inp">
					<label>昵称</label>
					<input type="text" placeholder="请输入昵称" id="nickname" name="nickname" class="user" data-role="none">
				</div>
				
				<div class="login-inp">
					<label>帐号</label>
					<input type="text" placeholder="" id="username" name="username" onkeyup="value=value.replace(/[^\w\.]/ig,'')" data-role="none" >
				</div>
				
				<div class="login-inp">
					<label>邮箱</label>
					<input type="text" placeholder="" id="email" onkeyup="this.value=this.value.replace(/[^a-zA-Z0-9_\-@\.]/g,'')" name="email" class="lock" data-role="none">
				</div>
				
				<div class="login-inp">
					<label>密码</label>
					<input type="password" placeholder="" id="password" name="password" data-role="none">
				</div>
				
				<div class="login-inp">
					<label>确认密码</label>
					<input type="password" placeholder="" id="password2"  data-role="none">
				</div>
				
				<div class="login-inp" style="text-align: center; width: 50%; margin-left: 25%; ">
					<input style="font-size: 19px;" class="user_login" type="button" value="提交" data-ajax="false"  data-role="none"/>
				</div>
		</div>
		</form>
	</div>
	</body>
	<script src="js/mui.min.js"></script>
	<script src="js/app.js"></script>
	<script type="text/javascript" src="js/zepto.min.js"></script>
	<script type="text/javascript" src="js/util.js"></script>
	<script>
		/*var options = $("#security").find("option:checked");
		console.log(options[0]);*/
		document.body.addEventListener('touchmove', function(event) {	
		    // 判断默认行为是否可以被禁用
		    if (event.cancelable) {
		        // 判断默认行为是否已经被禁用
		        if (!event.defaultPrevented) {
		            event.preventDefault();
		        }
		    }			
		}, false);
		//创建提示操作对象
		
		var param = {title:'提示',text:'',callback:function(){this.remove();}}
		
		/*$("#security").on('change',function(){
			var options = $("#security option:checked");
			console.log(options);
			console.log(options.val());
		});*/
		//绑定点击事件,注册
		$(".user_login").on('touchend',function(){
			var nickname = $("#nickname").val();
			var email=$("#email").val();
			console.log(nickname);
			console.log(email);
			if(!nickname){
				param.text = "请输入昵称！";
				iAlert(param);
				return;
			}
			console.log(checkUsername());
			var flag = checkemail()&&checkPwd()&&checkUsername();
			console.log(flag);
			if(flag){
				var data = $('form').serialize();
				console.log(data);
				$.ajax({
					"url": "http://192.168.0.150/WBPlatform/user/handle_register.do",
					"data": data,
					"type": "POST", 
					"dataType": "json", 
					"xhrFields": {
	           			withCredentials: true
	       			},
	       			"crossDomain": true,
					"success": function(obj) {
						if(obj.state==1){
							param.text = obj.message;
							iAlert(param);
							location.href="index.html";
						}else{
							param.text = obj.message;
							iAlert(param);
						}
					}
				});
			}
		});
		//检查邮箱
		function checkemail(){
			var email = $("#email").val();
			if(!email){
				param.text = "请输入邮箱！";
				iAlert(param);
				return false;
			}else{
				if(isMail(email)){
					var flag=false;
					$.ajax({
						"url": "http://192.168.0.150/WBPlatform/user/check_email.do",
						"data": "email=" + email,
						"type": "POST", 
						"dataType": "json", 
						"async": false,
						"xhrFields": {
		           			withCredentials: true
		       			},
		       			"crossDomain": true,
						"success": function(obj) {
							if(obj.state==1){
								console.log(obj.message);
								flag = true;
							}else{
								param.text = obj.message;
								iAlert(param);}
						}
					});
					return flag;
				}
			}
		};
		//账号验证
		function checkUsername(){
			var username = $("#username").val();
			if(!username){
				param.text = "请输入账号！";
				iAlert(param);
				return false;
			}else{
				var flag=false;
				$.ajax({
					"url": "http://192.168.0.150/WBPlatform/user/check_username.do",
					"data": "username=" + username,
					"type": "GET", 
					"dataType": "json", 
					"async": false,
					"xhrFields": {
	           			withCredentials: true
	       			},
	       			"crossDomain": true,
					"success": function(obj) {
						if(obj.state==1){
							console.log(obj.message);
							flag = true;
						}else{
							param.text = obj.message;
							iAlert(param);
						}
					}
				});
				return flag;
			}
		};
		//验证密码是否一致
		function checkPwd(){
			var pwd = $("#password").val();
			var pwd2 = $("#password2").val();
			if(pwd && pwd == pwd2){
				return true;
			}
			param.text = "两次输入的密码不一致！";
			iAlert(param);
			return false;
		}
		//验证是否是邮箱
		function isMail(str){
			var reg = /^[A-Za-z0-9]+@[a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+)+$/;
			if(reg.test(str)){
				return true;
			}
			param.text = "请输入正确的邮箱！";
			iAlert(param);
			return false;
		} 
	</script>
</html>
